package com.example.wordpoitldemo;

import cn.hutool.core.collection.CollectionUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import com.example.wordpoitldemo.domain.WordModel;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: xh
 * @date: 2023/11/15 10:14
 */
@Service
public class WordServiceImpl implements WordService{
    /**
     * 导出word模板
     * @return
     */
    @Override
    public XWPFTemplate generateWordXWPFTemplate() {
        //获取resources目录下的模板文件
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("word/template.docx");
        // 在这里使用file进行操作，例如读取文件内容
        XWPFTemplate template = XWPFTemplate.compile(inputStream);
        WordModel wordModel=new WordModel();
        wordModel.setName("张三");
        wordModel.setAge("18");
        wordModel.setSex("男");
        wordModel.setHobbies("唱歌和打篮球");

        //趋势图
        String[] abscissaData={"01", "02","03","04","05","06","07","08","09","10",
                "11","12","13","14","15","16","17","18","19","20",
                "21","22","23","24","25","26","27","28","29","30"};
        Double[] chargingCasesOrdinateData={
                159309.69, 164566.38,156819.87,148378.92,123710.59,176014.0,154682.63,154635.78,154635.78,154635.78,
                159309.69, 164566.38,156819.87,148378.92,123710.59,176014.0,154682.63,154635.78,154635.78,154635.78,
                159309.69, 164566.38,156819.87,148378.92,123710.59,176014.0,154682.63,154635.78,154635.78,154635.78,
        };
        wordModel.setTrendChart(creatLineChart("当月收入折线图","日收入（RMB）",abscissaData,chargingCasesOrdinateData));

        //表格
        //科室开单排名（前十科室）
        RowRenderData tenDepartmentsTableHead= Rows.of("排序", "科室名称","开单量").textColor("FFFFFF")
                .bgColor("4472C4").center().create();
        //创建表格list
        List<RowRenderData> tenDepartmentsRowList=new ArrayList<>();
        int order=1;
        List<Map<String, Object>> tenDepartmentsTableDate = getTenDepartmentsTableDate();
        if (CollectionUtil.isNotEmpty(tenDepartmentsTableDate)){
            for (Map<String, Object> tenDepartments : tenDepartmentsTableDate) {
                tenDepartmentsRowList.add(Rows.create(order+"",tenDepartments.get("departmentName")+"",tenDepartments.get("boldQuantity")+""));
                order++;
            }
        }
        wordModel.setTable(createTable(tenDepartmentsTableHead,tenDepartmentsRowList));
        return template.render(wordModel);
    }

    /**
     * 生成折线图
     *
     * @param title        图表标题
     * @param name         当前系列名称
     * @param abscissaData 横坐标数据
     * @param ordinateData 纵坐标数据
     * @return
     */
    private ChartMultiSeriesRenderData creatLineChart(String title, String name, String[] abscissaData, Double[] ordinateData) {
        ChartMultiSeriesRenderData chart = Charts
                .ofLine3D(title, abscissaData)
                .addSeries(name, ordinateData)
                .create();
        return chart;
    }

    /**
     * 获取科室开单排名前十的数据
     * @return
     */
    private List<Map<String,Object>> getTenDepartmentsTableDate() {
        List<Map<String,Object>> tenDepartmentsTableDate=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        map.put("departmentName","测试1");
        map.put("boldQuantity","50");
        tenDepartmentsTableDate.add(map);

        Map<String,Object> map1=new HashMap<>();
        map1.put("departmentName","测试2");
        map1.put("boldQuantity","500");
        tenDepartmentsTableDate.add(map1);
        return tenDepartmentsTableDate;
    }

    /**
     *
     * @param headRow 表头
     * @param tableData  表格数据
     * @return
     */
    private TableRenderData createTable(RowRenderData headRow, List<RowRenderData> tableData) {
        // 构造表格渲染数据  科室开单排名（前十科室）
        TableRenderData table = Tables.of(headRow).center().create();
        // 准备表格数据
        if (CollectionUtil.isNotEmpty(tableData)){
            for (RowRenderData  renderData : tableData) {
                table.addRow(renderData);
            }
        }
        return table;
    }
}
